﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DcsGauges.DataStructures;
using DcsGauges.Gui.Gauges.Interfaces;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using DcsGauges.Gui.Gauges.ValueConverters;
using DcsGauges.Gui.Rendering;
using DcsGauges.Shared.DataStructures;

namespace DcsGauges.Gui.Gauges
{
    class CircularGaugeWithSubRotator : GuiRenderableBase, IMultiValueGauge
    {
        protected URect                         subRotatorArea;
        protected Texture2D                     subRotatorNeedle            = null;
        protected IGaugeValueConverter<float>   subRotatorConverter         = null;

        protected DcsProperty[]                dataProperties              = null;
        protected CircularGauge                 baseGauge                   = null;
        protected float                         subRotatorValue             = 0.0f;

        public CircularGaugeWithSubRotator(GuiRenderableBase parent, URect area, DcsProperty rotatorDataProperty, DcsProperty subRotatorDataProperty,  
            IGaugeValueConverter<float> valueConverter, IGaugeValueConverter<float> subRotatorValueConverer, 
            Texture2D subRotatorNeedle, Texture2D gaugeBackground, Texture2D gaugeNeedle, 
            URect subRotatorArea) : base(parent, area)
        {
            this.subRotatorArea                 = subRotatorArea;
            this.subRotatorNeedle               = subRotatorNeedle;
            this.subRotatorConverter            = subRotatorValueConverer;

            this.dataProperties                 = new DcsProperty[] { rotatorDataProperty, subRotatorDataProperty };
            this.baseGauge                      = new CircularGauge(this, area, rotatorDataProperty, valueConverter, gaugeBackground, gaugeNeedle);
        }

        public override void Render(SpriteBatch spriteBatch, GraphicsDevice device, Microsoft.Xna.Framework.Rectangle parentRect, float globalZIndex)
        {
            this.baseGauge.Render(spriteBatch, device, parentRect, globalZIndex);
            Rectangle controlRect = this.baseGauge.ViewportRect;

            if (this.subRotatorNeedle != null)
            {
                float rotation = (float)this.subRotatorConverter.Convert(this.subRotatorValue);
                Rectangle subRotatorRect = this.CalcViewportRect(subRotatorArea, controlRect, subRotatorNeedle.Bounds);
                subRotatorRect.X += subRotatorRect.Width / 2;
                subRotatorRect.Y += subRotatorRect.Height / 2;
                spriteBatch.Draw(this.subRotatorNeedle, subRotatorRect, null, Color.White, rotation, 
                    new Vector2(this.subRotatorNeedle.Width / 2, this.subRotatorNeedle.Height / 2), SpriteEffects.None, this.NextFrontIndex(globalZIndex));
            }
        }

        public Shared.DataStructures.DcsProperty[] Properties
        {
            get 
            { 
                return this.dataProperties;
            }
        }

        public void SetValue(DcsProperty property, float value)
        {
            if(property == this.baseGauge.DataProperty) this.baseGauge.Value = value;
            else this.subRotatorValue = value;
        }

        public float GetValue(DcsProperty property)
        {
            if(property == this.baseGauge.DataProperty) return this.baseGauge.Value;
            else return this.subRotatorValue;
        }
    }
}
